package Week.w145;

import common.TreeNode;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/07/15 18:48
 */
public class problem02 {
    TreeNode treeNode=null;
    public TreeNode lcaDeepestLeaves(TreeNode root) {
        if(root==null){
            return null;
        }
        getResult(root);
        return treeNode;
    }

    private void getResult(TreeNode root) {
        if(root==null){
            return;
        }
        int left=maxDepth(root.left);
        int right=maxDepth(root.right);
        if(left==right){
            treeNode=root;
        }
        else if(left>right){
            getResult(root.left);
        }else {
            getResult(root.right);
        }

    }

    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

}
